<template>
  <div class="app-container">
    <pageTools :show-left="true">
      <template #left>共123条数据</template>
      <template #right>
        <el-button size="small" type="warning" @click="$router.push('/employees/import')">导入</el-button>
        <el-button size="small" type="danger" @click="exportToExcel">导出</el-button>
        <el-button size="small" type="primary" @click="addEmployee">新增员工</el-button>
      </template>
    </pageTools>
    <!-- 页面布局 -->
    <el-card>
      <el-table
        :data="employeeList"

        style="width: 100%"
      >
        <el-table-column label="序号" sortable="" type="index" />
        <el-table-column label="姓名" sortable="" prop="username" />
        <el-table-column label="工号" sortable="" prop="workNumber" />
        <el-table-column label="聘用形式" sortable="" :formatter="formatter" prop="formOfEmployment" />
        <el-table-column label="部门" sortable="" prop="departmentName" />
        <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
          <template v-slot="scope">
            {{ scope.row.timeOfEntry|formDate }}

            <template /></template></el-table-column>
        <el-table-column label="账户状态" sortable="" prop="enableState">
          <template v-slot="scope">
            <el-switch

              v-model="scope.row.enableState"
              :active-value="1"
              :inactive-value="2"
              active-color="#13ce66"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" sortable="" fixed="right" width="280">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="$router.push(`/employees/detail/${scope.row.id}`)">查看</el-button>
            <el-button type="text" size="small">转正</el-button>
            <el-button type="text" size="small">调岗</el-button>
            <el-button type="text" size="small">离职</el-button>
            <el-button type="text" size="small">角色</el-button>
            <el-button type="text" size="small" @click="delemployee(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 末尾 -->
      <el-row type="flex" justify="center" align="middle">
        <el-pagination
          layout="prev,pager,next"
          :page-size="page.size"
          :current-page="page.page"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <!-- 引入弹出层 -->
    <addEmployee
      :show-dialog="showDialog"
      @closedialog="showDialog=false"
      @reload="getemployeeList()"
    />
  </div>
</template>

<script>
// 引入弹出层
import addEmployee from './components/add-employee.vue'
import { export_json_to_excel } from '@/utils/Export2Excel'
import { getemployeeList, delEmployee } from '@/api/employees'
export default {
  components: {
    addEmployee
  },
  data() {
    return {
      page: {
        page: 1,
        size: 5
      },
      total: 0,
      employeeList: [],
      showDialog: false

    }
  },
  // 获取员工数据
  created() {
    this.getemployeeList()
  },
  methods: {
    async getemployeeList() {
      const res = await getemployeeList(this.page)
      this.total = res.total
      this.employeeList = res.rows
      console.log(res)
    },
    // 当前页改变时
    handleCurrentChange(val) {
      console.log(`这是第${val}页`)
      this.page.page = val
      this.getemployeeList()
    },
    formatter(row, col, cell, index) {
      const constArray = [
        { id: 1, value: '正式员工' },
        { id: 2, value: '非正式员工' }

      ]
      const obj = constArray.find(item => item.id === cell)
      return obj ? obj.value : '未知'
    },
    // 点击导出时处理
    async exportToExcel() {
      const res = await getemployeeList({ page: 1, size: this.total })
      // 导出所需数据
      const headers = { '姓名': 'username', '工号': 'workNumber', '聘用形式': 'formOfEmployment', '部门': 'departmentName', '入职时间': 'timeOfEntry', '账户状态': 'enableState' }
      // console.log(res.rows)
      // 处理导出的内容：导出的内容是数组套数组，而获取到的res.rows是数组套对象
      const data = res.rows.map(item => {
        const newArray = []
        for (const key in headers) {
          const newKey = headers[key]
          newArray.push(item[newKey])
        }
        return newArray
      })
      // 引入导出excel函数
      export_json_to_excel({
        // 导出的表头
        header: Object.keys(headers),
        // 导出的内容
        data

      })
    },
    // 点击新增时
    addEmployee() {
      this.showDialog = true
    },
    // 点击删除时
    async delemployee(row) {
      await this.$confirm('是否确认删除')
      await delEmployee(row.id)
      this.$message.success('删除成功')
      this.getemployeeList()
    }

  }

}
</script>

<style>

</style>
